using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document.EnvironmentalOntology;
using Xeml.Document;
using Xeml.Document.Contracts;
using Mpi.Common.Collections;

namespace Xid.Controls.ParameterEditCtrls
{
    public partial class CycleEditorCtrl : UserControl
    {
        private IDynamicOntologyTerm _paramSpec;
        private string _context;
        Dictionary<DynamicValue, CycleEntryEditCtrl> ctrls = new Dictionary<DynamicValue, CycleEntryEditCtrl>();
        public IDynamicOntologyTerm ParameterSpec
        {
            get { return _paramSpec; }
            set { _paramSpec = value; }
        }

        public string Context
        {
            get
            {
                return _context;
            }
            set
            {
                _context = value;
            }
        }

        public CycleEditorCtrl()
        {
            InitializeComponent();
            //AddCycleEntryEditor(new Value(TimeSpan.FromHours(1),"1"),false);
        }

        private Cycle cycle;
        public void SetupCycle(Cycle c)
        {
            if (this.cycle != null)
            {
                cycle.Values.ItemAdded -= new EventHandler<ItemEventArgs<DynamicValueBase>>(Values_ItemAdded);

                cycle.Values.CollectionChanged -= new EventHandler(Values_CollectionChanged);
            }
            foreach (CycleEntryEditCtrl cec in this.ctrls.Values)
            {
                cec.DropMe -= new EventHandler(DropEntry);
                this.Controls.Remove(cec);
            }
            this.ctrls.Clear();


            cycle = c;
            this.SuspendLayout();
            foreach (DynamicValue v in c.Values)
            {

                AddCycleEntryEditor(v, true);

            }
            this.ResumeLayout(true);
            //OnGrowthOrShrink();


            cycle.Values.ItemAdded += new EventHandler<ItemEventArgs<DynamicValueBase>>(Values_ItemAdded);
            cycle.Values.CollectionChanged += new EventHandler(Values_CollectionChanged);
            kryptonLabelLength.Values.ExtraText = c.Length.ToString();
        }

        void Values_CollectionChanged(object sender, EventArgs e)
        {
            kryptonLabelLength.Values.ExtraText = this.cycle.Length.ToString();
        }

        void Values_ItemAdded(object sender, ItemEventArgs<DynamicValueBase> e)
        {
            AddCycleEntryEditor(e.Item as DynamicValue, false);
        }

        private void AddCycleEntryEditor(DynamicValue v, bool batchMode)
        {
            if (!batchMode)
                this.SuspendLayout();
            CycleEntryEditCtrl cec = new CycleEntryEditCtrl();
            cec.SetupSpec(_paramSpec,_context);
            cec.Value = v;
            cec.Dock = DockStyle.Bottom;
            this.ctrls.Add(v, cec);
            this.Controls.Add(cec);
            cec.DropMe += new EventHandler(DropEntry);
            if (this.ctrls.Count == 1)
            {
                foreach (CycleEntryEditCtrl x in this.ctrls.Values)
                {
                    x.EnableDrop = false;
                }
            }
            else if (this.ctrls.Count > 1)
            {
                foreach (CycleEntryEditCtrl x in this.ctrls.Values)
                {
                    x.EnableDrop = true;
                }
            }
            if (!batchMode)
            {
                this.ResumeLayout(true);
                //OnGrowthOrShrink();
            }
        }

        void DropEntry(object sender, EventArgs e)
        {
            this.SuspendLayout();
            CycleEntryEditCtrl cec = sender as CycleEntryEditCtrl;
            this.cycle.Values.Remove(cec.Value);
            cec.DropMe -= new EventHandler(DropEntry);
            this.ctrls.Remove(cec.Value);
            this.Controls.Remove(cec);
            cec.Dispose();

            if (this.ctrls.Count == 1)
            {
                foreach (CycleEntryEditCtrl x in this.ctrls.Values)
                {
                    x.EnableDrop = false;
                }
            }

            this.ResumeLayout(true);
           // OnGrowthOrShrink();
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            DynamicValue v = _paramSpec.DefaultValue(_context, TimeSpan.FromHours(12));

            this.cycle.Values.Add(v);
            // AddCycleEntryEditor(v);
        }

        //public event EventHandler GrowthOrShrink;
        //protected virtual void OnGrowthOrShrink()
        //{
        //    if (GrowthOrShrink != null)
        //        GrowthOrShrink(this, new EventArgs());
        //}
    }
}
